﻿package 
{
	//WEEK 05
	import flash.display.MovieClip;
	import flash.display.FrameLabel;
	import flash.events.*;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.geom.Point;
	//bullet class
	import bullet;
	

	public class GameManager extends MovieClip{
		static public const MENU:String = "menu";
		static public const GAME:String = "game";
		static public const END:String = "end";
		
		//Keys
		static public const LEFT:int = Keyboard.LEFT;
		static public const RIGHT:int = Keyboard.RIGHT;
		static public const DOWN:int = Keyboard.DOWN;
		static public const UP:int = Keyboard.UP;
		//wk5 fire button
		static public const FIRE:int = Keyboard.SPACE;

		// constant for the speed of the bullet
		public const BULLETPOWER:int = 9;		
		
		//Movement Properties
		private var _power:int = 3;
		private var _leftPressed:Boolean = false;
		private var _rightPressed:Boolean = false;
		private var _upPressed:Boolean = false;
		private var _downPressed:Boolean = false;

		private var _characterNextX:int;
		private var _characterNextY:int;
		
		// store the direction the character is facing
		private var _isFacing:String="";

		//wk5 guard list 
		private var _guards:Array = new Array();
		
		//shooting
		private var _bulletRef:bullet = null;
		private var _bulletDirection:String = "";
		
		
		public function GameManager(){
			//setup frames of main movie to send an event when they are reached
			enumerateFrameLabels();
			addEventListener(MENU, setupMenu,false,0,true);
			addEventListener(GAME, setupGame,false,0,true);
			addEventListener(END, setupEnd,false,0,true);
		}
		
		private function dispatchFrameEvent():void{
			stop();
			dispatchEvent(new Event(currentLabel));
		}
		private function enumerateFrameLabels():void{
			for each (var label:FrameLabel in currentLabels)
				addFrameScript(label.frame-1, dispatchFrameEvent);
		}
		private function setupMenu(e:Event){
			test_button.button_text.text = "Start";
			test_button.addEventListener(MouseEvent.CLICK, aClick,false,0,true);
		}
		
		private function setupGame(e:Event){
			end_button.button_text.text = "Win";
			end_button.addEventListener(MouseEvent.CLICK, bClick,false,0,true);
			// add keyboard listeners
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKDown,false,0,true);
			stage.addEventListener(KeyboardEvent.KEY_UP,onKUp,false,0,true);
			// add the main game loop listener
			addEventListener(Event.ENTER_FRAME,update,false,0,true);

			//wk5 add the guard movie clips to the guards list
			_guards.push(guard01);
			_guards.push(guard02);
		}
		private function setupEnd(e:Event){
			
			start_button.button_text.text = "Menu";
			start_button.addEventListener(MouseEvent.CLICK, cClick,false,0,true);
			
		}
		
		private function cleanUpGame()
		{
			//Remove Keyboard listeners and Enter frame listener
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP,onKDown);
			removeEventListener(Event.ENTER_FRAME,update);
		}
		
		private function exitGame()
		{
			cleanUpGame();
			gotoAndPlay(END);
		}
		
		
		//START Button Management Code		
		private function aClick(e:Event){
			gotoAndPlay(GAME);
		}
		private function bClick(e:Event){
			gotoAndPlay(END);
		}
		private function cClick(e:Event){
			gotoAndPlay(MENU);
		}
		//END Button Management
		
		//START Input Management
		private function onKDown(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case LEFT: 
				_leftPressed = true;
				_isFacing = "left";
				break;
				case RIGHT: 
				_rightPressed = true;
				_isFacing = "right";
				break;
				case DOWN: 
				_downPressed = true;
				_isFacing = "up";
				break;
				case UP: 
				_upPressed = true;
				_isFacing = "down";
				break;

			}
			
		}
		
		private function onKUp(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case LEFT: 
				character.gotoAndPlay("start");
				_leftPressed = false;
				_isFacing = "";
				break;
				case RIGHT: 
				character.gotoAndPlay("start");
				_rightPressed = false;
				_isFacing = "";
				break;
				case DOWN: 
				_downPressed = false;
				_isFacing = "";
				break;
				case UP: 
				_upPressed = false;
				_isFacing = "";
				break;
				case FIRE:
				shootBullet();
				break;
			}
		}
		
		
		//END Input Management
		//character.x -= power;	character.gotoAndPlay("left");
		//character.x += power;	character.gotoAndPlay("right");
		
		//MAIN GAME LOOP
		private function update(e:Event)
		{
			handleKeyInput();
			checkCharacterCollision();
			movecharacter();
			moveBullet();
		}
		
		private function checkCharacterCollision()
		{
			// Bottom Left - check clip 
			if(mazeClip.hitTestPoint(_characterNextX+11,_characterNextY+28,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+11,_characterNextY+28,true))
				{
					_characterNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_characterNextX+11,character.y+28,true))
				{
					_characterNextY = character.y;
				}
				
			}
			// Bottom Right
			if(mazeClip.hitTestPoint(_characterNextX+24,_characterNextY+28,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+24,_characterNextY+28,true))
				{
					_characterNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_characterNextX+24,character.y+28,true))
				{
					_characterNextY = character.y;
				}

			}
			// Top Left
			if(mazeClip.hitTestPoint(_characterNextX+11,_characterNextY+4,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+11,_characterNextY+4,true))
				{
					_characterNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_characterNextX+11,character.y+4,true))
				{
					_characterNextY = character.y;
				}
			}
			// Top Right
			if(mazeClip.hitTestPoint(_characterNextX+24,_characterNextY+4,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+24,_characterNextY+4,true))
				{
					_characterNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_characterNextX+24,character.y+4,true))
				{
					_characterNextY = character.y;
				}
			}
			
			// check the fires
			// Bottom Left
			if(firesClip.hitTestPoint(_characterNextX+11,_characterNextY+28,true))
			{
				_characterNextX = 0;
				_characterNextY = 32;
			}
			// Bottom Right
			if(firesClip.hitTestPoint(_characterNextX+24,_characterNextY+28,true))
			{
				_characterNextX = 0;
				_characterNextY = 32;
			}
			// Top Left
			if(firesClip.hitTestPoint(_characterNextX+11,_characterNextY+4,true))
			{
				_characterNextX = 0;
				_characterNextY = 32;
			}
			// Top Right
			if(firesClip.hitTestPoint(_characterNextX+24,_characterNextY+4,true))
			{
				_characterNextX = 0;
				_characterNextY = 32;
			}
			
			// check exit
			// Bottom Left
			if(exitClip.hitTestPoint(_characterNextX+11,_characterNextY+28,true))
			{
				exitGame();
			}
			//Bottom Right
			if(exitClip.hitTestPoint(_characterNextX+24,_characterNextY+28,true))
			{
				exitGame();
			}
			// Top Left
			if(exitClip.hitTestPoint(_characterNextX+11,_characterNextY+4,true))
			{
				exitGame();
			}
			// Top Right
			if(exitClip.hitTestPoint(_characterNextX+24,_characterNextY+4,true))
			{
				exitGame();
			}

			//wk5
			// check the guards
			for each (var guard in _guards){
				// Bottom Left
				if(guard.hitTestPoint(_characterNextX+11,_characterNextY+28,true))
				{
					_characterNextX = 0;
					_characterNextY = 32;
				}
				// Bottom Right
				if(guard.hitTestPoint(_characterNextX+24,_characterNextY+28,true))
				{
					_characterNextX = 0;
					_characterNextY = 32;
				}
				// Top Left
				if(guard.hitTestPoint(_characterNextX+11,_characterNextY+4,true))
				{
					_characterNextX = 0;
					_characterNextY = 32;
				}
				// Top Right
				if(guard.hitTestPoint(_characterNextX+24,_characterNextY+4,true))
				{
					_characterNextX = 0;
					_characterNextY = 32;
				}
			}
		}
		
		private function handleKeyInput()
		{
			// set the _next position to the current position
			_characterNextX = character.x;
			_characterNextY = character.y;

			if(_leftPressed)
			{
				_characterNextX = character.x - _power;
				character.nextFrame();
				if(character.currentLabel != "left")
				{
					character.gotoAndStop("left");
				}

			}
			if(_rightPressed)
			{
				_characterNextX = character.x + _power;
				
				character.nextFrame();
				if(character.currentLabel != "right")
				{
					character.gotoAndStop("right");
				}
			}
			if(_upPressed)
			{
				_characterNextY = character.y - _power;
			}
			if(_downPressed)
			{
				_characterNextY = character.y + _power;
			}
			
		}
		
		private function movecharacter()
		{
			if(_characterNextX>=0 && _characterNextX<=500)
			{
				character.x = _characterNextX;
			}
			if(_characterNextY>=0 && _characterNextY<=350)
			{
				character.y = _characterNextY;
			}
		}
		
		private function shootBullet(){
			// check to see if there is a bullet already in play
			if(_bulletRef==null){
				// set the bullets direction to the same as the characters current facing
				_bulletDirection = _isFacing;
				//if the character is facing in a direction 
				if(_bulletDirection!="")
				{
					//add bullet to the stage and register it in the bullet variable
					_bulletRef = new bullet();
					addChild(_bulletRef);
					// start the new bullet in the middle of the character
					_bulletRef.x = character.x + 16;
					_bulletRef.y = character.y + 16;
				}
				
			}
		}
		
		private function moveBullet(){
			//if the there is something in the bullet variable then move it
			if(_bulletRef!=null){
				switch(_bulletDirection)
				{

					case "up":
					_bulletRef.y-= BULLETPOWER;
					break;

					case "down":
					_bulletRef.y+= BULLETPOWER;
					break;
					
					case "right":
					_bulletRef.x+= BULLETPOWER;
					break;

					case "left":
					_bulletRef.x-= BULLETPOWER;
					break;
					
				}
				
				if(mazeClip.hitTestPoint(_bulletRef.x,_bulletRef.y,true))
				{	
					removeChild(_bulletRef);
					_bulletRef = null;
					return
				}
				
				//firesClip
				if(firesClip.hitTestPoint(_bulletRef.x,_bulletRef.y,true))
				{	
					removeChild(_bulletRef);
					_bulletRef = null;
					return
				}			
				//exitclip
				if(exitClip.hitTestPoint(_bulletRef.x,_bulletRef.y,true))
				{	
					removeChild(_bulletRef);
					_bulletRef = null;
					return
				}
				for each (var guard in _guards)
				{
					if(guard.hitTestPoint(_bulletRef.x,_bulletRef.y,true))
					{	
						removeChild(_bulletRef);
						_bulletRef = null;
						//guard.visible = false;
						removeChild(guard);
						return
					}
				}
				
				if(_bulletRef.x<0 || _bulletRef.x>540)
				{
					removeChild(_bulletRef);
					_bulletRef = null;
					return
				}
				if(_bulletRef.y<0 || _bulletRef.y>380)
				{
					removeChild(_bulletRef);
					_bulletRef = null;
					return
				}
			}
		}
		
	}
}